package com.strawcom.charmring.ui;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.TextView;

import com.strawcom.charmring.BaseActivity;
import com.strawcom.charmring.C;
import com.strawcom.charmring.R;
import com.strawcom.charmring.bluetooth.RingBleService;
import com.strawcom.charmring.bluetooth.RingBleService.LocalBinder;
import com.strawcom.charmring.util.LogUtil;
import com.strawcom.charmring.util.PreferenceUtil;
import com.strawcom.charmring.view.MProgressView;
import com.strawcom.charmring.view.MProgressView.OnProgressChangedListener;
import com.strawcom.charmring.view.TitleView;
import com.strawcom.charmring.view.togglebutton.ToggleButton;
import com.strawcom.charmring.view.togglebutton.ToggleButton.OnToggleChanged;

public class SwitchActivity extends BaseActivity implements OnToggleChanged {

	private TitleView titleView;
	private ToggleButton tbPhone, tbLocation, tbLoss;

	private MProgressView viewLightTime, viewLossTime;
	private TextView tvLayoutLightTimeTip, tvLayoutLossTimeTip;

	private RingBleService bleService;

	private final int MIN_VALUE = 1, MAX_VALUE = 60;

	@Override protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_switch);
		super.onCreate(savedInstanceState);

		titleView = (TitleView) findViewById(R.id.title_view);
		titleView.setTitleText(R.string.notice);
		tbPhone = (ToggleButton) findViewById(R.id.toggleButton_phone);
		tbLocation = (ToggleButton) findViewById(R.id.toggleButton_location);
		tbLoss = (ToggleButton) findViewById(R.id.toggleButton_loss);

		int lightTime = PreferenceUtil.readInt(this, C.sp.switch_light_time, C.app.default_ble_light_time);
		tvLayoutLightTimeTip = (TextView) findViewById(R.id.tv_layout_light_time_tip);
		tvLayoutLightTimeTip.setText(String.format(getString(R.string.light_tip), lightTime));
		viewLightTime = (MProgressView) findViewById(R.id.view_light_time);
		viewLightTime.setCurrentValue(lightTime, MIN_VALUE, MAX_VALUE);
		viewLightTime.setListener(new OnProgressChangedListener() {
			@Override public void onProgressChanged(float progress) {
				int lightTime = (int) viewLightTime.getCurrentValue();
				tvLayoutLightTimeTip.setText(String.format(getString(R.string.light_tip), "" + lightTime));
			}
		});
		viewLightTime.setThickness(0.5f);

		int lossTime = PreferenceUtil.readInt(this, C.sp.switch_loss_time, C.app.default_ble_loss_time);
		tvLayoutLossTimeTip = (TextView) findViewById(R.id.tv_layout_loss_time_tip);
		tvLayoutLossTimeTip.setText(String.format(getString(R.string.loss_tip_1), lossTime));
		viewLossTime = (MProgressView) findViewById(R.id.view_loss_time);
		viewLossTime.setCurrentValue(lossTime, MIN_VALUE, MAX_VALUE);
		viewLossTime.setListener(new OnProgressChangedListener() {
			@Override public void onProgressChanged(float progress) {
				int lossTime = (int) viewLossTime.getCurrentValue();
				tvLayoutLossTimeTip.setText(String.format(getString(R.string.loss_tip_1), "" + lossTime));
			}
		});
		viewLossTime.setThickness(0.5f);

		boolean bPhone = PreferenceUtil.readBoolean(this, C.sp.switch_phone_calls_reminding, true);
		if (bPhone) {
			tbPhone.setToggleOn();
		}
		else {
			tbPhone.setToggleOff();
		}
		viewLightTime.setEnable(bPhone);
		boolean bAlarm = PreferenceUtil.readBoolean(this, C.sp.switch_location_alarm, true);
		if (bAlarm) {
			tbLocation.setToggleOn();
		}
		else {
			tbLocation.setToggleOff();
		}
		boolean bLost = PreferenceUtil.readBoolean(this, C.sp.switch_mobile_lost_proof, true);
		if (bLost) {
			tbLoss.setToggleOn();
		}
		else {
			tbLoss.setToggleOff();
		}
		viewLossTime.setEnable(bLost);

		tbPhone.setOnToggleChanged(this);
		tbLocation.setOnToggleChanged(this);
		tbLoss.setOnToggleChanged(this);

		bindRingBleService();
	}

	@Override protected void onDestroy() {
		super.onDestroy();
		unbindRingBleService();
	}
	/** 在按返回键的时候的保存 */
	@Override public void onBackPressed() {
		boolean callsRemind = tbPhone.toggleState();
		boolean locationShare = tbLocation.toggleState();
		boolean lossPrevent = tbLoss.toggleState();
		int lightTime = (int) viewLightTime.getCurrentValue();
		int lossTime = (int) viewLossTime.getCurrentValue();

		bleService.setDeviceBasicInfo(bleService.getCurrAddress(), callsRemind, locationShare, lossPrevent, lightTime, lossTime);
		super.onBackPressed();
	}

	@Override public void onToggle(boolean on, ToggleButton tb) {
		switch (tb.getId()) {
		case R.id.toggleButton_phone:
			PreferenceUtil.write(this, C.sp.switch_phone_calls_reminding, on);
			viewLightTime.setEnable(on);
			break;
		case R.id.toggleButton_location:
			PreferenceUtil.write(this, C.sp.switch_location_alarm, on);
			break;
		case R.id.toggleButton_loss:
			PreferenceUtil.write(this, C.sp.switch_mobile_lost_proof, on);
			viewLossTime.setEnable(on);
			break;
		}
	}

	private void bindRingBleService() {
		Intent intent = new Intent(this, RingBleService.class);
		bindService(intent, conn, BIND_AUTO_CREATE);
	}

	private void unbindRingBleService() {
		unbindService(conn);
	}

	private final ServiceConnection conn = new ServiceConnection() {
		@Override public void onServiceDisconnected(ComponentName name) {
			LogUtil.e("MainActivity onServiceDisconnected()");
			bleService = null;
		}

		@Override public void onServiceConnected(ComponentName name, IBinder service) {
			LogUtil.e("MainActivity onServiceConnected()");
			bleService = ((LocalBinder) service).getService();
		}
	};
}
